/**
 *  ItemStack.java : IceBird project
 *  19:28:44 
 */
package icebird.compiler.ncomp.vs;


/**
 * Represents stack of items.
 * @author Sergey Shulepoff[Knott]
 */
public class ItemStack {

	protected Item[] stack;
	protected int peek;
	
	public void reset() {
		stack = new Item[8];
		peek = 0;
	}
	
	public ItemStack() {
		reset();
	}
	
	private final void grow() {
		Item[] tmp = new Item[stack.length * 2];
		System.arraycopy(stack, 0, tmp, 0, stack.length);
		
		stack = tmp;
	}
	
	public void pop() {
		if (peek <= 0) {
			throw new IllegalStateException();
		}
		
		peek--;
	}
	
	public void pop(Item i) {
		Item g = getPeek();
		
		if (g != i) {
			throw new IllegalStateException();
		}
		
		pop();
	}
	
	public Item getPeek() {
		return stack[peek - 1];
	}
	
	public void push(Item i) {
		if (peek == stack.length) 
			grow();
		
		stack[peek++] = i;
	}
}
